package com.flyou.ccnucenter.ui.frgment.HenuBeside.lostFind;


import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.adapter.HenuBesideLostRecycleViewAdapter;
import com.flyou.ccnucenter.domain.Lost;
import com.flyou.ccnucenter.ui.activity.henuBeside.lostFind.LostFoundDetialActivity;
import com.flyou.ccnucenter.ui.frgment.BaseFragment;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.MyLog;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.widget.loading.LoadingView;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;


public class OtherFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private LoadingView loadingView;
    private RelativeLayout failedView;
    private SwipeRefreshLayout mSwipeLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private HenuBesideLostRecycleViewAdapter adapter;
    private View mPaentView;
    private String objectId;
    private int currentPage = 0;
    private int pageSize = 10;
    private List<Lost> lostList;

    @Override
    protected View onCreatView(LayoutInflater inflater, ViewGroup container) {
        mPaentView = inflater.inflate(R.layout.fragment_book, container, false);

        return mPaentView;
    }

    @Override
    protected void initData() {
        objectId = (String) SPUtils.get(context, "objectId", "");
        GetLostFromNet();
//        GetDataFromNet();
    }



    @Override
    protected void initView() {
        mSwipeLayout = (SwipeRefreshLayout) mPaentView.findViewById(R.id.swipView);
        recyclerView = (RecyclerView) mPaentView.findViewById(R.id.recycleView);


        loadingView = (LoadingView) mPaentView.findViewById(R.id.loadingView);
        loadingView.setLoadingText("正在校园内搜索物品……");
        loadingView.setVisibility(View.VISIBLE);
        failedView = (RelativeLayout) mPaentView.findViewById(R.id.failed_view);


        linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);

        mSwipeLayout = (SwipeRefreshLayout) mPaentView.findViewById(R.id.swipView);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
    }


    /*
  *从服务器获取笔记数据
  */
    private void GetLostFromNet() {

        MyLog.d(getClass().getSimpleName() + ":::::" + currentPage);
        BmobQuery<Lost> query = new BmobQuery<Lost>();
        query.addWhereEqualTo("type", Lost.TYPE_OTHER);//根据用户id查询
        query.addWhereEqualTo("isHide","false");
        query.order("-createdAt");//根据更新时间排序
        query.setLimit(pageSize);//每页显示数据
        query.setSkip(currentPage * pageSize);//分页

        query.findObjects(context, new FindListener<Lost>() {
            @Override
            public void onStart() {
                if (failedView.isShown()) {
                    failedView.setVisibility(View.GONE);
                }
                super.onStart();
            }

            @Override
            public void onFinish() {
                if (mSwipeLayout.isRefreshing()) {
                    mSwipeLayout.setRefreshing(false);
                }
                if (loadingView.isShown()) {
                    loadingView.setVisibility(View.GONE);
                }
                super.onFinish();
            }

            @Override
            public void onSuccess(List<Lost> object) {
                if (object.size() == 0) {
                    if (currentPage != 0)
                        Toast.makeText(context, "没有更多数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (currentPage == 0) {
                    lostList = object;
                    adapter = new HenuBesideLostRecycleViewAdapter(context, object);
                    /*
                    * item 动画
                    * */
                    recyclerView.setItemAnimator(new SlideInLeftAnimator());
                    /*
                    * recycleview动画
                    * */

                    recyclerView.setAdapter(new ScaleInAnimationAdapter(adapter));

                    recyclerView.addOnScrollListener(new onRecycleViewScollListener());

                    adapter.setOnItemClickLitener(new onItiemClickListener());
                } else {
                    lostList.addAll(object);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (currentPage == 0) {
                    failedView.setVisibility(View.VISIBLE);
                }
                Toast.makeText(context, BmobErrorCodeUtils.getMessage(code, msg), Toast.LENGTH_SHORT).show();
            }

        });

    }


    /*
       * 滑动事件的监听
       * */
    public class onRecycleViewScollListener extends RecyclerView.OnScrollListener {
        private int lastVisibleItem = 0;
        private boolean isSlidingToLast = false;
        public onRecycleViewScollListener() {
            super();
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

            super.onScrollStateChanged(recyclerView, newState);
            if (isSlidingToLast) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                currentPage++;
                GetLostFromNet();
            }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                isSlidingToLast = true;
            } else {
                isSlidingToLast = false;
            }
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    }

    public class onItiemClickListener implements HenuBesideLostRecycleViewAdapter.OnItemClickLitener {
        @Override
        public void onItemClick(View view, int position) {

            Intent intent = new Intent(context, LostFoundDetialActivity.class);
            Lost lostFound = lostList.get(position);
            intent.putExtra("lostFound",lostFound);
            startActivity(intent);
        }

        @Override
        public void onItemLongClick(View view, final int position) {

        }
    }


    @Override
    public void onRefresh() {
        currentPage = 0;
        GetLostFromNet();
    }
    @Override
    public void onPause() {
        currentPage=0;
        super.onPause();
    }
}

